<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
 contributor license agreements.  See the NOTICE file distributed with
 this work for additional information regarding copyright ownership.
 The ASF licenses this file to You under the Apache License, Version 2.0
 (the "License"); you may not use this file except in compliance with
 the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

-->

<!--
Usage notes:

To build Win32EventLogAppender.dll using MinGW, place mingw\bin on path for Windows or install mingw
package on other platforms.  Build will execute gcc and windres on Windows and
i586-mingw32msvc-gcc and i586-mingw32msvc-windres on other platforms.

To build Win32EventLogAppender.dll using Microsoft tools, run the appropriate vcvars for the
target platform and specify msbuild as the target on the ant command line.

-->


<project name="ntdll" default="build" basedir=".">
  <property name="target.dir" value="target"/>
  <property name="object.dir" value="target/obj"/>
  <property name="src.dir" location="${basedir}"/>
  <property name="failonerror" value="false"/>
  <property name="failifexecutionfails" value="${failonerror}"/>
  <property environment="env"/>
  <property name="root.dir" location="${basedir}/../.." />
  <property file="${root.dir}/build/svn.properties" />	
  <property file="${root.dir}/build/version.properties" />

  <target name="clean" description="Deletes generated files">
    <delete dir="${target.dir}"/>
    <delete file="${src.dir}/version.rc" />
    <delete file="${src.dir}/MSG00001.bin" />
    <delete file="${src.dir}/EventLogCategories.h" />
    <delete file="${src.dir}/EventLogCategories.rc" />
    <delete file="${src.dir}/Win32EventLogAppender.res" />
  </target>

  <!--
        Regenerates resource files from EventLogCategories.mc.  Avoids
        need for Microsoft Platform SDK unless you are modifying EventLogCategories.mc
    -->
  <target name="mc" description="Update EventLogCategories.rc from .mc">
    <exec executable="mc">
      <arg value="-h"/>
      <arg file="${basedir}"/>
      <arg value="-r"/>
      <arg file="${basedir}"/>
      <arg file="EventLogCategories.mc"/>
    </exec>
  </target>



  <target name="windres">
    <condition property="mingw-prefix" value="">
      <os family="windows"/>
    </condition>
    <property name="mingw-prefix" value="i586-mingw32msvc-"/>
    <mkdir dir="${object.dir}"/>
    <exec executable="${mingw-prefix}windres"
   dir="${src.dir}"
   resultproperty="windres_status"
   failonerror="${failonerror}"
   failifexecutionfails="${failifexecutionfails}">
      <arg value="-o"/>
      <arg file="${object.dir}/Win32EventLogAppender.o"/>
      <arg file="${src.dir}/Win32EventLogAppender.rc"/>
    </exec>
    <condition property="windres_success">
      <equals arg1="${windres_status}" arg2="0"/>
    </condition>
    <condition property="do_compile">
      <and>
        <isset property="windres_success"/>
      </and>
    </condition>
  </target>

  <target name="compile" depends="windres" if="do_compile">
    <exec executable="${mingw-prefix}gcc">
      <arg value="-Wall"/>
      <arg value="-Wl,--kill-at"/>
      <arg value="-fno-rtti"/>
      <arg value="-fno-exceptions"/>
      <arg value="-s"/>
      <arg value="-I${object.dir}"/>
      <arg value="-shared"/>
      <arg file="${object.dir}/Win32EventLogAppender.o"/>
      <arg value="-o"/>
      <arg file="${target.dir}/Win32EventLogAppender.dll"/>
    </exec>
  </target>

  <target name="windres-echo" depends="windres" unless="windres_success">
    <echo>${mingw-prefix}windres could not be found or failed, Win32EventLogAppender.dll build skipped.</echo>
  </target>

  <target name="build" depends="compile, windres-echo" description="Build with MinGW gcc">
  </target>

  <target name="rc">
    <copy file="${src.dir}/version.rc.template" tofile="${src.dir}/version.rc">
      <filterchain>
        <expandproperties />
      </filterchain>
    </copy>
    <exec executable="rc"
      dir="${src.dir}"
      resultproperty="rc_status"
      failonerror="${failonerror}"
      failifexecutionfails="${failifexecutionfails}">
      <arg file="${src.dir}/Win32EventLogAppender.rc"/>
    </exec>
    <condition property="rc_success">
      <equals arg1="${rc_status}" arg2="0"/>
    </condition>
    <condition property="do_cl">
      <and>
        <isset property="rc_success"/>
      </and>
    </condition>
  </target>

  <target name="cl" depends="rc" if="do_cl">
    <mkdir dir="${object.dir}"/>
    <property name="object.location" location="${object.dir}"/>
    <exec executable="cl" dir="${target.dir}">
      <arg value="/O2"/>
      <arg value="/GR-"/>
      <arg value="/LD"/>
      <arg value="/I${object.location}"/>
      <arg value="/I${src.dir}"/>
      <arg file="${src.dir}/Win32EventLogAppender.res"/>
      <arg file="${src.dir}/Win32EventLogAppender.def"/>
      <arg value="/FeWin32EventLogAppender.dll"/>
      <arg value="/link /NOENTRY"/>
    </exec>
  </target>

  <target name="rc-echo" depends="rc" unless="rc_success">
    <echo>rc could not be found or failed, Win32EventLogAppender.dll build skipped.</echo>
  </target>

  <target name="msbuild" depends="mc, cl, rc-echo" description="Build using Microsoft tools">
  </target>

</project>
